package io.renren.modules.oa.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.dingtalk.DingtalkUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.oa.entity.UserConfigEntity;
import io.renren.modules.oa.redis.DingtalkRedis;
import io.renren.modules.oa.service.UserConfigService;
import io.renren.modules.sys.shiro.ShiroUtils;



/**
 * 全局配置
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2018-10-26 09:01:20
 */
@RestController
@RequestMapping("oa/userconfig")
public class UserConfigController {
    @Autowired
    private UserConfigService userConfigService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("oa:userconfig:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userConfigService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("oa:userconfig:info")
    public R info(@PathVariable("id") Long id){
        UserConfigEntity userConfig = userConfigService.selectById(id);
        return R.ok().put("userConfig", userConfig);
    }
    
    /**
     * 全局配置
     */
    @RequestMapping("/infoconfig")
    public R infoconfig(){
    	Long merchantId = ShiroUtils.getUserId();	//商户编号
    	UserConfigEntity userConfig = userConfigService.selectByMerchantId(merchantId);
        //初始化全局配置信息
    	if(userConfig == null){ 
        	userConfig = new UserConfigEntity();
        	userConfig.setMerchantId(merchantId);
        	userConfig.setType("1");
        	userConfig.setCreateTime(new Date());
        	userConfigService.insert(userConfig);
        }
    	return R.ok().put("userConfig", userConfig);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("oa:userconfig:save")
    public R save(@RequestBody UserConfigEntity userConfig){
    	
        userConfigService.insert(userConfig);
        
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("oa:userconfig:update")
    public R update(@RequestBody UserConfigEntity userConfig){
    	
        ValidatorUtils.validateEntity(userConfig);
        userConfigService.updateAllColumnById(userConfig);//全部更新
        if(userConfig.getDingToken() != null && userConfig.getDingAesKey() != null && userConfig.getDingUrl() != null){
        	DingtalkUtils.registerCallBack(DingtalkRedis.getAccessToken(userConfig.getMerchantId()), userConfig.getDingToken(), userConfig.getDingAesKey(), userConfig.getDingUrl());
        }
        Long merchantId = ShiroUtils.getUserId();	//清空缓存
        DingtalkRedis.delAccessToken(merchantId);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("oa:userconfig:delete")
    public R delete(@RequestBody Long[] ids){
        userConfigService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
